<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 帮助类
 * @package  module
 * @author   allen.qiang
 * @version 1.0
 */

class sysHelp {

    private $dbca;

    public function __construct( $dbca ) {
        $this->dbca = $dbca;
    }

    /**
     * 帮助文章列表
     * @param $arg_array
     * @return array
     */
    function searchHelpArticleList(&$arg_array){
        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        if (isset($arg_array['articleID']) && is_numeric($arg_array['articleID'])) {
            $sql .= ' AND articleID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['articleID'];
        }

        if (isset($arg_array['sort']) && in_array($arg_array['sort'],array(0,1))) {
            $sql .= ' AND sort = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['sort'];
        }

        if (isset($arg_array['s_articleTitle']) && !empty($arg_array['s_articleTitle'])) {
            $sql .= ' AND articleTitle LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['s_articleTitle']) . '%';
        }

        if (isset($arg_array['s_bigCategoryID']) && is_numeric($arg_array['s_bigCategoryID'])) {
            $sql .= ' AND bigCategoryID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['s_bigCategoryID'];
        }

        if (isset($arg_array['s_midCategoryID']) && is_numeric($arg_array['s_midCategoryID'])) {
            $sql .= ' AND midCategoryID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['s_midCategoryID'];
        }

        if (isset($arg_array['s_smallCategoryID']) && is_numeric($arg_array['s_smallCategoryID'])) {
            $sql .= ' AND smallCategoryID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['s_smallCategoryID'];
        }

        if (isset($arg_array['useFlg']) && in_array($arg_array['useFlg'], array(0, 1))) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        if ($sql) {
            $sql = ' WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_help_article ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM sys_help_article ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'creator'        => $item->creator,
                    'timeStamp'      => $item->timeStamp,
                    'createTime'     => $item->createTime,
                    'articleID'      => $item->articleID,
                    'branch_code'    => $item->branch_code,
                    'articleTitle'   => $item->articleTitle,
                    'articleContent' => $item->articleContent,
                    'sort'            => $item->sort,
                    'useFlg'          => $item->useFlg,
                    'bigCategoryID'  => $item->bigCategoryID,
                    'midCategoryID'  => $item->midCategoryID,
                    'smallCategoryID'=> $item->smallCategoryID
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }
    }

    /**
     * 帮助文章更新或添加
     * @param $arg_array
     * @return array
     */
    public  function addHelpArticle(&$arg_array) {

        //接收参数
        $articleID        = $arg_array['articleID'];
        $timeStamp        = $arg_array['timeStamp'];
        $sort             = $arg_array['sort'];
        $bigCategoryID    = $arg_array['bigCategoryID'];
        $midCategoryID    = $arg_array['midCategoryID'];
        $smallCategoryID  = $arg_array['smallCategoryID'];
        $creator          = $arg_array['creator'];
        $articleTitle     = $arg_array['articleTitle'];
        $articleContent   = $arg_array['description'];

        //实例化查询类
        $faqcategory_obj = new entity('sys_help_article');

        //检查ID是否为空，信息是否存在
        if( !empty($articleID) ){

            $faqcategory_obj->articleID  = $articleID;

            if( !$this->dbca->fetch($faqcategory_obj) ){
                return array(  'code'=>404, 'message'=>'没有找到你修改的分类信息，请刷新后重试');
            }
        }

        $n_timeStamp = $faqcategory_obj->timeStamp  ;

        $faqcategory_obj->creator           = $creator ;
        $faqcategory_obj->sort              = $sort ;
        $faqcategory_obj->bigCategoryID    = $bigCategoryID ;
        $faqcategory_obj->midCategoryID    = $midCategoryID ;
        $faqcategory_obj->smallCategoryID  = $smallCategoryID ;
        $faqcategory_obj->articleTitle     = $articleTitle ;
        $faqcategory_obj->articleContent   = $articleContent;
        $faqcategory_obj->useFlg            = 1;

        if( $n_timeStamp != $timeStamp){
            return array(  'code'=>400 , 'message'=>'你所编辑的文章已被修改，请刷新页面' );
        }

        //保存到数据库
        try{

            $this->dbca->save( $faqcategory_obj );

            if( $articleID ){
                return array( 'code'=>200  , 'message'=>'更新成功' );

            }else{
                return array('code'=>200  ,'message'=>'保存成功' );

            }

        }catch(Exception $e){

            error_log( $e->getMessage() );
            return array('code'=>500  , 'message'=>'系统错误，请与管理员联系'  );

        }
    }

    /**
     * 删除帮助文章信息
     * @param $arg_array
     */
    public function delHelpArticle (&$arg_array) {

        $delArr = $arg_array['delArr'];
        if( empty($delArr) ){

            return array('code'=>400 , 'message'=>'请选择要删除的信息' );
        }

        //统计不存在的信息的数目
        $errorNum = 0;

        //记录要删除的entity对象
        $deleteObjArr = array();

        //检查要删除的信息是否全部存在
        foreach( $delArr as $articleID ){

            //检查信息是否存在
            $check_obj = new entity('sys_help_article');
            $check_obj->articleID = $articleID;

            if( !$this->dbca->fetch( $check_obj ))

                $errorNum++;

            $deleteObjArr[] = $check_obj;

        }

        if( $errorNum > 0 ){

            return array('message'=>'所选部分文章已不存在，请刷新页面' , 'code'=>400  );
        }

        try{

            //开启事务
            $this->dbca->beginTransaction();

            //循环删除
            foreach( $deleteObjArr as $delobj ){

                $this->dbca->delete( $delobj );

            }

            //提交事务
            $this->dbca->commit();

            return array('message'=>'删除成功', 'code'=>200);


        }catch( Exception $e ){

            //回滚事务
            $this->dbca->rollBack();

            error_log($e->getMessage());
            return array('message'=>'系统错误，请与管理员联系', 'code'=>500);
        }

    }

    public function changeHelpFlg($arg_array){
        $articleID = $arg_array['articleID'];
        $useFlg = $arg_array['useFlg'];
        //检查信息是否存在
        $check_obj = new entity('sys_help_article');
        $check_obj->articleID = $articleID;

        if( !$this->dbca->fetch($check_obj) ){
            return array('message'=>'没有找到当前信息' , 'code'=>404  );
        }

        //检查状态是否被修改
        $n_useFlg = $check_obj->useFlg  ;

        if( $useFlg == 1 && $n_useFlg == 0){
            return array('message'=>'你所选择的文章已处于<b>禁用</b>状态<br>请重新刷新页面' , 'code'=>400  );

        }

        if( $useFlg == 0 && $n_useFlg == 1){
            return array('message'=>'你所选择的文章已处于<b>启用</b>状态<br>请重新刷新页面' , 'code'=>400  );

        }

        //修改状态
        if( $n_useFlg == 0 ){
            $check_obj->useFlg = 1 ;

        }else{
            $check_obj->useFlg = 0 ;
        }

        try{

            $this->dbca->save( $check_obj );

            if( $n_useFlg == 0 ){
                return array('message'=>'启用成功' , 'code'=>200 );

            }else{
                return array('message'=>'禁用成功' , 'code'=>200 );

            }

        }catch(Exception $e){

            error_log( $e->getMessage() );
            return array('message'=>'系统错误，请与管理员联系' , 'code'=>500  );

        }

    }

    /**
     * 查询帮助中心大分类
     * @param $arg_array
     * @return array|null
     */
    public function searchHelpBigCategory(&$arg_array){

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //分类ID
        if (isset($arg_array['bigCategoryID']) && is_numeric($arg_array['bigCategoryID'])) {
            $sql .= ' AND bigCategoryID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['bigCategoryID'];
        }

        //分类名称
        if (isset($arg_array['bigCategoryName']) && is_numeric($arg_array['bigCategoryName'])) {
            $sql .= ' AND bigCategoryName like ?';
            $params_type .= 's';
            $params_array[] = '%'.$arg_array['bigCategoryName'].'%';
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_help_big_category ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM sys_help_big_category ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {

                $midNum = $this->dbca->stmt_query('select count(*) as num from sys_help_mid_category where bigCategoryID = ? ','i',$item->bigCategoryID);

                $hasMid = $midNum[0]->num?1:0;

                $item_array[] = array(
                    'createTime'  => $item->createTime,
                    'bigCategoryID'          =>  $item->bigCategoryID,
                    'bigCategoryName' => $item->bigCategoryName,
                    'description'       =>  $item->description,
                    'sort'        =>  $item->sort,
                    'hasMid' => $hasMid
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }


    }

    /**
     * 查询帮助中心中分类
     * @param $arg_array
     * @return array|null
     */
    public function searchHelpMidCategory(&$arg_array){

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //分类ID
        if (isset($arg_array['midCategoryID']) && is_numeric($arg_array['midCategoryID'])) {
            $sql .= ' AND midCategoryID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['midCategoryID'];
        }

        //大分类ID
        if (isset($arg_array['bigCategoryID'])  && is_numeric($arg_array['bigCategoryID'])) {
            $sql .= ' AND bigCategoryID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['bigCategoryID'];
        }

        //分类名称
        if (isset($arg_array['midCategoryName']) && is_numeric($arg_array['midCategoryName'])) {
            $sql .= ' AND midCategoryName like ?';
            $params_type .= 's';
            $params_array[] = '%'.$arg_array['midCategoryName'].'%';
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_help_mid_category ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM sys_help_mid_category ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {

                $smallNum = $this->dbca->stmt_query('select count(*) as num from sys_help_small_category where midCategoryID = ? ','i',$item->midCategoryID);

                $hasSma = $smallNum[0]->num?1:0;

                $item_array[] = array(
                    'createTime'  => $item->createTime,
                    'midCategoryID'          =>  $item->midCategoryID,
                    'midCategoryName' => $item->midCategoryName,
                    'description'       =>  $item->description,
                    'sort'        =>  $item->sort,
                    'bigCategoryID' => $item->bigCategoryID,
                    'hasSma' => $hasSma
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 查询帮助中心小分类
     * @param $arg_array
     * @return array|null
     */
    public function searchHelpSmallCategory(&$arg_array){

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //分类ID
        if (isset($arg_array['midCategoryID']) && is_numeric($arg_array['midCategoryID'])) {
            $sql .= ' AND midCategoryID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['midCategoryID'];
        }

        //小分类ID
        if (isset($arg_array['smallCategoryID']) && is_numeric($arg_array['smallCategoryID'])) {
            $sql .= ' AND smallCategoryID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['smallCategoryID'];
        }

        //分类名称
        if (isset($arg_array['smallCategoryName']) && is_numeric($arg_array['smallCategoryName'])) {
            $sql .= ' AND smallCategoryName like ?';
            $params_type .= 's';
            $params_array[] = '%'.$arg_array['smallCategoryName'].'%';
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_help_small_category ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM sys_help_small_category ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {

                $mid_category =  $this->dbca->stmt_query("select * from sys_help_mid_category where midCategoryID = ? ",'i',$item->midCategoryID);

                $item_array[] = array(
                    'createTime'         => $item->createTime,
                    'midCategoryID'      => $item->midCategoryID,
                    'smallCategoryID'    => $item->smallCategoryID,
                    'smallCategoryName'  => $item->smallCategoryName,
                    'description'         => $item->description,
                    'sort'                 => $item->sort,
                    'bigCategoryID'       => $mid_category[0]->bigCategoryID,
                    's_midCategoryName'   => $mid_category[0]->midCategoryName
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 删除帮助中心分类
     * @param $arg_array
     * @return array
     */
    public function deleteHelpCenterCategory(&$arg_array){
        if(!isset($arg_array['delete_str'])){
            return array('code'=>400,'message'=>'参数错误');
        }

        $delete_arr = explode('_',$arg_array['delete_str']);

        $bigArr = array();//大分类ID数组

        $midArr = array();//中分类ID数组

        $smlArr = array();//小分类ID数组

        switch(count($delete_arr)){
            case 1://删除小分类

                $bid = array('bigCategoryID'=>$delete_arr[0]);

                $result = $this->searchHelpMidCategory($bid);

                $bigArr[] = $delete_arr[0];

                if($result){

                    foreach($result as $val){

                        $midArr[] = $val['midCategoryID'];

                        $mid = array('midCategoryID'=>$val['midCategoryID']);

                        $smallResult = $this->searchHelpSmallCategory($mid);

                        if($smallResult){

                            foreach($smallResult as $smallVal){
                                $smlArr[] =  $smallVal['smallCategoryID'];
                            }

                        }

                    }
                }

                $this->dbca->stmt_query('delete from sys_help_big_category where bigCategoryID = ?','i',$delete_arr[0]);
                break;
            case 2://删除中分类

                $midArr[] = $delete_arr[1];

                $mid = array('midCategoryID'=>$delete_arr[1]);

                $smallResult = $this->searchHelpSmallCategory($mid);

                if($smallResult){

                    foreach($smallResult as $smallVal){
                        $smlArr[] =  $smallVal['smallCategoryID'];
                    }

                }

                $this->dbca->stmt_query('delete from sys_help_mid_category where midCategoryID = ?','i',$delete_arr[1]);
                break;
            case 3://删除小分类

                $smlArr[] = $delete_arr[2];

                $this->dbca->stmt_query('delete from sys_help_small_category where smallCategoryID = ?','i',$delete_arr[2]);
                break;
        }

        return array('code'=>200,'message'=>'删除成功','bigArr'=>$bigArr,'midArr'=>$midArr,'smlArr'=>$smlArr);
    }

    /**
     * 保存 编辑大分类
     * @param $arg_array
     * @return array|null
     */
    public function saveBigHelpCategory(&$arg_array){

        if(!isset($arg_array['bigCategoryName']) || empty($arg_array['bigCategoryName'])){
            return array('code'=>400,'message'=>'大分类名称不能为空');
        }

        if(!isset($arg_array['sort']) ||!is_numeric($arg_array['sort'])){
            return array('code'=>400,'message'=>'请填写正确排序序号');
        }

        try {

            $big = new entity('sys_help_big_category');

            if(isset($arg_array['editStr']) && !empty($arg_array['editStr'])){
                $big->bigCategoryID = $arg_array['editStr'];
                if(!$this->dbca->fetch($big)){
                    return array('code'=>400,'message'=>'该分类不存在');
                }
            }
            $big->creator = $arg_array['creator'];
            $big->bigCategoryName = $arg_array['bigCategoryName'];
            $big->sort = $arg_array['sort'];
            $big->description = $arg_array['description'];

            $this->dbca->save($big);

            //排序序号
            $maxSortOrder = $this->dbca->stmt_query('SELECT IF(MAX(sort) IS NULL ,10 , MAX(sort)+1) AS max_sortOrder FROM sys_help_big_category');

            return array('code'=>200,'message'=>'帮助中心分类保存成功','insert_id'=>($maxSortOrder[0]->max_sortOrder));

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 保存 编辑中分类
     * @param $arg_array
     * @return array|null
     */
    public function saveMidHelpCategory($arg_array){

        if(!isset($arg_array['m_bigCategoryID']) || empty($arg_array['m_bigCategoryID'])){
            return array('code'=>400,'message'=>'请选择大分类');
        }

        if(!isset($arg_array['midCategoryName']) || empty($arg_array['midCategoryName'])){
            return array('code'=>400,'message'=>'中分类名称不能为空');
        }

        if(!isset($arg_array['m_sort']) ||!is_numeric($arg_array['m_sort'])){
            return array('code'=>400,'message'=>'请填写正确排序序号');
        }

        try {


            $big = new entity('sys_help_mid_category');

            if(isset($arg_array['editStr']) && !empty($arg_array['editStr'])){
                $editArr = explode('_',$arg_array['editStr']);
                $big->midCategoryID = $editArr[1];
                if(!$this->dbca->fetch($big)){
                    return array('code'=>400,'message'=>'该分类不存在');
                }
            }
            $big->creator = $arg_array['creator'];
            $big->midCategoryName = $arg_array['midCategoryName'];
            $big->sort = $arg_array['m_sort'];
            $big->description = $arg_array['m_description'];
            $big->bigCategoryID = $arg_array['m_bigCategoryID'];

            $this->dbca->save($big);

            //排序序号
            $maxSortOrder = $this->dbca->stmt_query('SELECT IF(MAX(sort) IS NULL ,100 , MAX(sort)+1) AS max_sortOrder FROM sys_help_mid_category');

            return array('code'=>200,'message'=>'帮助中心分类保存成功','insert_id'=>(($maxSortOrder[0]->max_sortOrder)));

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 保存 编辑小分类
     * @param $arg_array
     * @return array|null
     */
    public function saveSmaHelpCategory($arg_array){

        if(!isset($arg_array['s_bigCategoryID']) || empty($arg_array['s_bigCategoryID'])){
            return array('code'=>400,'message'=>'请选择大分类');
        }

        if(!isset($arg_array['s_midCategoryID']) || empty($arg_array['s_midCategoryID'])){
            return array('code'=>400,'message'=>'请选择中分类');
        }

        if(!isset($arg_array['smallCategoryName']) || empty($arg_array['smallCategoryName'])){
            return array('code'=>400,'message'=>'请输入小分类名称');
        }

        if(!isset($arg_array['s_sort']) ||!is_numeric($arg_array['s_sort'])){
            return array('code'=>400,'message'=>'请填写正确排序序号');
        }

        try {


            $big = new entity('sys_help_small_category');

            if(isset($arg_array['editStr']) && !empty($arg_array['editStr'])){
                $editArr = explode('_',$arg_array['editStr']);
                $big->smallCategoryID = $editArr[2];
                if(!$this->dbca->fetch($big)){
                    return array('code'=>400,'message'=>'该分类不存在');
                }
            }

            $big->creator = $arg_array['creator'];
            $big->smallCategoryName = $arg_array['smallCategoryName'];
            $big->description = $arg_array['s_description'];
            $big->sort = $arg_array['s_sort'];
            $big->midCategoryID = $arg_array['s_midCategoryID'];

            $this->dbca->save($big);

            //排序序号
            $maxSortOrder = $this->dbca->stmt_query('SELECT IF(MAX(sort) IS NULL ,1000 , MAX(sort)+1) AS max_sortOrder FROM sys_help_small_category');

            return array('code'=>200,'message'=>'帮助中心分类保存成功','insert_id'=>(($maxSortOrder[0]->max_sortOrder)));

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    public function getHelpCategorySort($arg_array){

        $big_maxSortOrder = $this->dbca->stmt_query('SELECT IF(MAX(sort) IS NULL ,10 , MAX(sort)+1) AS max_sortOrder FROM sys_help_big_category');

        $mid_maxSortOrder = $this->dbca->stmt_query('SELECT IF(MAX(sort) IS NULL ,100 , MAX(sort)+1) AS max_sortOrder FROM sys_help_mid_category');

        $sma_maxSortOrder = $this->dbca->stmt_query('SELECT IF(MAX(sort) IS NULL ,1000 , MAX(sort)+1) AS max_sortOrder FROM sys_help_small_category');

        return array('big'=>$big_maxSortOrder[0]->max_sortOrder,'mid'=>$mid_maxSortOrder[0]->max_sortOrder,'sma'=>$sma_maxSortOrder[0]->max_sortOrder);
    }

}